package com.wmlive.hhvideo.heihei.discovery.presenter;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.base.BaseView;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.discovery.BannerListBean;
import com.wmlive.hhvideo.heihei.beans.discovery.TopicTypeListBean;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.networklib.entity.DCNetObserver;

import java.util.List;

/**
 * Created by lsq on 6/2/2017.
 * 发现页面的Presenter
 */

public class DiscoveryPresenter extends BasePresenter<DiscoveryPresenter.IDiscoveryView> {

    public DiscoveryPresenter(IDiscoveryView view) {
        super(view);
    }

    //偏移
    private int offset = 0;

    //获取Banner的数据
    public void getBanner() {
        if (InitCatchData.socialGetBanner() != null) {
            executeRequest(HttpConstant.TYPE_DISCOVERY_BANNER, getHttpApi().banner(InitCatchData.socialGetBanner()))
                    .subscribe(new DCNetObserver<BannerListBean>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BannerListBean response) {
                            if (viewCallback != null) {
                                viewCallback.onBannerOk(response.getBanners());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(HttpConstant.TYPE_DISCOVERY_BANNER, message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_DISCOVERY_BANNER);
        }
    }

    //获取Topic列表
    public void getTopicList(final boolean isRefresh) {
        if (InitCatchData.topicListTopic() != null) {
            executeRequest(HttpConstant.TYPE_DISCOVERY_TOPIC_HOME, getHttpApi().topicList(InitCatchData.topicListTopic(), isRefresh ? 0 : offset))
                    .subscribe(new DCNetObserver<TopicTypeListBean>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, TopicTypeListBean response) {
                            if (viewCallback != null) {
                                offset = response.getOffset();
                                viewCallback.onGetTopicListOk(isRefresh, response.getTopic_list(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_DISCOVERY_TOPIC_HOME : (HttpConstant.TYPE_DISCOVERY_TOPIC_HOME + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_DISCOVERY_TOPIC_HOME);
        }
    }

    public interface IDiscoveryView extends BaseView {
        void onBannerOk(List<BannerListBean.Banner> bannerList);

        void onGetTopicListOk(boolean isRefresh, List<TopicTypeListBean.TopicListBean> list, boolean hasMore);

    }
}
